package prototype;

import java.util.ArrayList;

/**
 * 深浅复制
 *
 * @author laigl 2017年7月11日
 *
 */
public class Test implements Cloneable {

	private ArrayList<String> nameList = new ArrayList<String>();

	public void add(String s) {
		this.nameList.add(s);
	}

	public ArrayList<String> get() {
		return this.nameList;
	}

	// 浅复制
//	@Override
//	public Test clone() {
//		try {
//			return (Test) super.clone();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return null;
//	}

	// 深复制
	@SuppressWarnings("unchecked")
	@Override
	public Test clone() {
		try {
			Test test = (Test) super.clone();
			test.nameList = (ArrayList<String>) this.nameList.clone();
			return test;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		Test test = new Test();
		test.add("aa");
		test.add("bb");
		System.out.println("test:::" + test.get());

		Test test2 = test.clone();
		test2.add("cc");
		System.out.println("test2:::" + test2.get());
		System.out.println("test:::" + test.get());

	}

}
